// pages/person/person.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data :{
      name:'',
      work:' ',
      num:' ',
      wx:' ',
      mail:' ',
      region:' ',
      place:' ',
      about:' ',
      img:'/img/touxiang.png',
    },
    id:0,
    status:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //注册监听器函数
    getApp().registerListener(this.onStatusChange.bind(this));
  },

  //全局变量status改变时调用该函数
  onStatusChange:function(){
    this.setData({
      status:getApp().globalData.status,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    if(getApp().globalData.status==1){
      that.setData({
        data:wx.getStorageSync('users'),
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  collect:function(){
    if(!wx.getStorageSync('users')){
      wx.showModal({
        title:'温馨提示',
        content:'请先登录'
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/collect/collect',
      })
    }
    
  },
  card:function(){
    if(wx.getStorageSync('users')){
      wx.navigateTo({
        url: '/pages/card/card',
      })
    }
    else{
      wx.showModal({
        title:'温馨提示',
        content:'请先登录'
      })
    }
    
  },
  theme:function(){
    wx.navigateTo({
      url: '/pages/theme/theme',
    })
  },

  company:function(){
    if(!wx.getStorageSync('users')){
      wx.showModal({
        title:'温馨提示',
        content:'请先登录'
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/company/company',
      })
    }
    
  },
  module:function(){
    if(!wx.getStorageSync('users')){
      wx.showModal({
        title:'温馨提示',
        content:'请先登录'
      })
    }
    else{
      wx.navigateTo({
        url: '/pages/module/module',
      })
    }
    
  },
  about:function(){
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  //用户登录or注册
  login_card:function(){
    var that = this;
    
    if(!wx.getStorageSync('user_id')){
      
      //注册,测试代码
      wx.getUserProfile({
        desc:'用于完善用户资料',
          success:(res)=>{
            var regname = res.userInfo.nickName;
            var regimg = res.userInfo.avatarUrl;
            that.setData({
              'data.img':regimg,
              'data.name':regname,
            })
            console.log('未申请前'+that.data.data);
          
            wx.request({
              url: `http://127.0.0.1:2020/api/reg`,
              method:'GET',
              data:that.data.data,
              header:{'content-type':'application/JSON;charset=UTF-8'},
              success:function(res){
                var arr = parseInt(res.data.split('为')[1]);
                that.setData({
                  id:arr,
                })  
              }
            })

            wx.showModal({
              title:'温馨提示',
              content:'注册成功，请确认登录',
              confirmText:'登录',
              cancelText:'取消',
              success:(res)=>{
                if(res.confirm){
                  console.log(that.data.id)
                  wx.request({
                    url: `http://127.0.0.1:2020/api/login?id=${that.data.id}`,
                    method:'GET',
                    header:{'content-type':'application/JSON;charset=UTF-8'},
                    success:function(res){
                      console.log(res.data)
                      that.setData({
                        data:res.data,
                        status:1,
                      })
                      wx.setStorageSync('users', res.data);
                      wx.setStorageSync('user_id', res.data.id);
                      wx.setStorageSync('status', 1);
                      //全局变量status改变
                      getApp().globalData.status=1;
                      getApp().triggerListeners();

                      wx.showModal({
                        title:'温馨提示',
                        content:'需要进行信息完善确保他人可以找到哦♪(･ω･)ﾉ',
                        confirmText:'修改信息',
                        cancelText:'取消',
                        success:(res)=>{
                          if(res.confirm){
                            wx.navigateTo({
                              url: '/pages/card/card',
                            })
                          }
                        }
                      })
                    }
                  })
                }
              }
            })
          
          }
          
          
          
      })
    }
    else{
      var loginid = wx.getStorageSync('user_id');
      wx.request({
        url: `http://127.0.0.1:2020/api/login?id=${loginid}`,
        method:'GET',
        header:{'content-type':'application/JSON;charset=UTF-8'},
        success:function(res){
          wx.setStorageSync('status', 1);
          // console.log(res.data)
          that.setData({
            data:res.data,
            status:1,
            id:res.data.id,
          })
          wx.setStorageSync('users', res.data);

          //全局变量status改变
          getApp().globalData.status=1;
          getApp().triggerListeners();

          wx.showModal({
            title:'温馨提示',
            content:'登陆成功',
            confirmText:'确认',
            cancelText:'取消',
            success:()=>{
              var com_name = wx.getStorageSync('users').place;
              wx.request({
                url: `http://127.0.0.1:2020/api/company?com_name=${com_name}`,
                method:'GET',
                header:{'content-type':'application/JSON;charset=UTF-8'},
                  success:function(res){
                  // console.log(res.data[0])
                  wx.setStorageSync('user_com', res.data[0])
                  }
              })
            }
          })
        }
      })
      
    }
  },

  out:function(){
    wx.setStorageSync('status', 0);
    var that = this;
    wx.showModal({
      title:'温馨提示',
      content:'是否退出登录',
      confirmText:'确认',
      cancelText:'取消',
      success:(res)=>{
        that.setData({
          data :{
            name:'',
            work:' ',
            num:' ',
            wx:' ',
            mail:' ',
            region:' ',
            place:' ',
            about:' ',
            img:'../../img/touxiang.png',
          },
          id:0,
          status:0
        })
        wx.removeStorageSync('users');
        wx.removeStorageSync('user_com');
        //全局变量status改变
        getApp().globalData.status=0;
        getApp().triggerListeners();
      }
    }) 
  },
})